package infod

import (
	"time"

	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/common"
	cinfod "gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/infod"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/ddd"
)

// @collname 信息集合(i_info)
type collInfo struct {
	InfoID       string                 `bson:"_id"`                    // 唯一标识
	Owner        string                 `bson:"owner"`                  // 所有者
	Org          string                 `bson:"org"`                    // 所属组织
	Title        string                 `bson:"title"`                  // 标题
	ColumnID     string                 `bson:"columnId"`               // 所属栏目
	Kind         int                    `bson:"kind"`                   // 信息类型
	SubTitle     string                 `bson:"subTitle"`               // 副标题标题
	ListMode     int                    `bson:"listMode"`               // 列表展示时的默认模式
	Icon         []string               `bson:"icon"`                   // 图标
	Banner       string                 `bson:"banner"`                 // 旗帜
	From         string                 `bson:"from"`                   // 来源
	Desc         string                 `bson:"desc"`                   // 简介
	Content      string                 `bson:"context"`                // 内容
	Extras       map[string]interface{} `bson:"extras"`                 // 扩展信息
	Tags         []string               `bson:"tags"`                   // 标签列表
	CreTime      time.Time              `bson:"creTime"`                // 创建时间
	Creator      string                 `bson:"creator"`                // 创建人
	UpdateTime   time.Time              `bson:"updateTime"`             // 更新时间
	Updater      string                 `bson:"updater"`                // 更新人
	DeleteTime   time.Time              `bson:"deleteTime,omitempty"`   // 删除时间
	Deleter      string                 `bson:"deleter,omitempty"`      // 删除人
	Status       cinfod.IStatus         `bson:"status"`                 // 状态
	IsPublic     int                    `bson:"isPublic"`               // 发布后的公开状态 0 公开 1 私有
	Weight       int                    `bson:"wieght"`                 // 权重值
	Checker      string                 `bson:"checker,omitempty"`      // 审核人
	CheckContent string                 `bson:"checkContent,omitempty"` // 审核内容
	CheckResult  int                    `bson:"checkResult,omitempty"`  // 审核结果 0 不通过 1 通过
	CheckTime    time.Time              `bson:"checkTime,omitempty"`    // 审核时间
	Publisher    string                 `bson:"publisher"`              // 发布人
	PublishTime  time.Time              `bson:"publishTime"`            // 发布时间

}

func (c collInfo) CollID() string {
	return c.InfoID
}
func (collInfo) CollName() string {
	return collNameInfo
}
func (c collInfo) IsCache() bool {
	return c.InfoID != "" && c.Status == cinfod.IStatusPublished
}
func (c collInfo) CacheExpire() time.Duration {
	return 600 * time.Second
}
func (c *collInfo) FullDesc(info cinfod.InfoDesc) {
	c.Owner = info.Owner
	c.Org = info.Org
	c.ColumnID = info.ColumnID
	c.Kind = info.Kind
	c.Title = info.Title
	c.SubTitle = info.SubTitle
	c.ListMode = info.ListMode
	c.Icon = info.Icon
	c.Banner = info.Banner
	c.From = info.From
	c.Desc = info.Desc
	c.Tags = info.Tags
	c.Content = info.Content
	c.Extras = info.Extras
}

func (c *collInfo) FullOperator(info common.Operator) {
	c.CreTime = info.CreTime
	c.Creator = info.Creator
	c.UpdateTime = info.UpdateTime
	c.Updater = info.Updater
	c.Deleter = info.Deleter
	c.DeleteTime = info.DeleteTime
}
func (c collInfo) FullChkOperator(info cinfod.ChkOperator) {
	c.Checker = info.Checker
	c.CheckContent = info.CheckContent
	c.CheckResult = info.CheckResult
	c.CheckTime = info.CheckTime
}
func (c collInfo) FullPubOperator(info cinfod.PubOperator) {
	c.Publisher = info.Publisher
	c.PublishTime = info.PublishTime
}
func (c *collInfo) FullStatus(info cinfod.InfoStatus) {

	c.Status = info.Status
	c.IsPublic = info.IsPublic
	c.Weight = info.Weight
}

func (c collInfo) ToStatus() cinfod.InfoStatus {
	return cinfod.InfoStatus{
		Status:   c.Status,
		IsPublic: c.IsPublic,
		Weight:   c.Weight,
	}
}
func (c collInfo) ToDesc() cinfod.InfoDesc {
	return cinfod.InfoDesc{
		Owner:    c.Owner,
		Org:      c.Org,
		ColumnID: c.ColumnID,
		Kind:     c.Kind,
		Title:    c.Title,
		SubTitle: c.SubTitle,
		ListMode: c.ListMode,
		Icon:     c.Icon,
		Banner:   c.Banner,
		From:     c.From,
		Desc:     c.Desc,
		Tags:     c.Tags,
		Content:  c.Content,
		Extras:   c.Extras,
	}
}
func (c collInfo) ToOperator() common.Operator {
	return common.Operator{
		CreTime:    c.CreTime,
		Creator:    c.Creator,
		Updater:    c.Updater,
		UpdateTime: c.UpdateTime,
		Deleter:    c.Deleter,
		DeleteTime: c.DeleteTime,
	}
}
func (c collInfo) ToChkOperator() cinfod.ChkOperator {
	return cinfod.ChkOperator{
		Checker:      c.Checker,
		CheckContent: c.CheckContent,
		CheckResult:  c.CheckResult,
		CheckTime:    c.CheckTime,
	}
}
func (c collInfo) ToPubOperator() cinfod.PubOperator {
	return cinfod.PubOperator{
		Publisher:   c.Publisher,
		PublishTime: c.PublishTime,
	}
}

func (c collInfo) ToAggregate() ddd.Aggregate {
	return c.ToInfoAggregate()

}
func (c collInfo) ToInfoAggregate() *cinfod.InfoAggregate {
	return &cinfod.InfoAggregate{
		InfoID:      c.InfoID,
		InfoDesc:    c.ToDesc(),
		Operator:    c.ToOperator(),
		InfoStatus:  c.ToStatus(),
		PubOperator: c.ToPubOperator(),
		ChkOperator: c.ToChkOperator(),
	}

}

// @collname 信息操作集合（i_infooplog）
type collInfoOpLog struct {
	ID       string    `bson:"_id"`      // 唯一标识
	InfoID   string    `bson:"infoID"`   // 信息标识
	Operator string    `bson:"operator"` // 操作人
	OpTime   time.Time `bson:"opTime"`   // 操作时间
	Action   string    `bson:"action"`   // 操作行为描述
}

func (c collInfoOpLog) CollID() string {
	return c.ID
}
func (collInfoOpLog) CollName() string {
	return collNameInfoOpLog
}
func (c collInfoOpLog) ToDesc() cinfod.InfoOplogDesc {
	return cinfod.InfoOplogDesc{
		InfoID:   c.InfoID,
		Operator: c.Operator,
		OpTime:   c.OpTime,
		Action:   c.Action,
	}
}

func (c collInfoOpLog) ToAggregate() ddd.Aggregate {
	return c.ToInfoOplogAggregate()

}
func (c collInfoOpLog) ToInfoOplogAggregate() *cinfod.InfoOplogAggregate {
	return &cinfod.InfoOplogAggregate{
		InfoOplogID:   c.ID,
		InfoOplogDesc: c.ToDesc(),
	}

}
